<?php
/**
 * User zgh
 * Email 1803897171@qq.com
 * Date 2021/7/11
 **/

namespace app\admin\controller;


use app\admin\service\AdminRoleService;
use app\admin\service\AdminService;
use app\common\Annotation\ControllerAnnotation;
use app\common\Annotation\NodeAnotation;
use app\common\controller\BackendController;
use app\common\exception\ServiceException;
use think\facade\Request;

/**
 * @ControllerAnnotation(title="管理员管理",auth=2)
 * Class AdminController
 * @package app\admin\controller
 */
class AdminController extends BackendController
{

    /**
     * @NodeAnotation(title="列表",auth=2)
     * @return mixed|\think\response\Json
     */
    public function index()
    {
        if (Request::isAjax()) {
            $result = AdminService::getList(Request::param());
            return success_json_response("成功", $result['dataList'], ['count' => $result['total']]);
        } else {
            return $this->fetch();
        }
    }

    /**
     * @NodeAnotation(title="添加",auth=2)
     * @return mixed|\think\response\Json
     * @throws ServiceException
     */
    public function add()
    {
        $params = Request::param();
        if (Request::isAjax()) {
            $params = filter_params($params, ['password']);
            $res = AdminService::save($params);
            if ($res) {
                return success_json_response("添加成功", ["id" => $res]);
            } else {
                return json_response("添加失败");
            }
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="修改",auth=2)
     * @return mixed|\think\response\Json
     * @throws ServiceException
     */
    public function edit()
    {
        $params = Request::param();
        if (Request::isAjax()) {
            $params = filter_params($params, ['username', 'password']);
            $res = AdminService::save($params);
            return $res ? success_json_response("修改成功", ["id" => $res]) : json_response("修改失败");
        }
        $model = AdminService::getByID($params['id']);
        $this->assign("model", $model);
        $this->assign("admin_role_arr", AdminRoleService::getAdminRoles($params['id']));
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="删除",auth=2)
     * @return \think\response\Json
     */
    public function del()
    {
        if (Request::isAjax()) {
            $res = AdminService::del(Request::param());
            return $res ? success_json_response("删除成功") : json_response("删除失败");
        }
    }

    /**
     * @NodeAnotation(title="设置密码",auth=2)
     * @return mixed|\think\response\Json
     * @throws ServiceException
     */
    public function password()
    {
        $params = Request::param();
        if (Request::isAjax()) {
            if (!isset($params['id']) || (int)$params['id'] == 1) {
                throw  new ServiceException("id非法");
            }
            $password = $params["password"];
            return AdminService::userPassword($password, $params['id']) ? success_json_response("密码设置成功") : json_response("密码设置失败");
        } else {
            $model = AdminService::getByID($params['id']);
            $this->assign("model", $model);
            return $this->fetch();
        }
    }


    /**
     * @NodeAnotation(title="修改",auth=1)
     * @return mixed|\think\response\Json
     * @throws \app\common\exception\ServiceException
     */
    public function user_setting()
    {
        if (Request::isAjax()) {
            $params = Request::param();
            return AdminService::userSetting($params) ? success_json_response("修改成功") : json_response("修改失败");
        } else {
            $model = AdminService::getByID(AdminService::getAdminId());
            $this->assign("model", $model);
            return $this->fetch();
        }
    }


    /**
     * @NodeAnotation(title="修改密码",auth=1)
     * @return mixed|\think\response\Json
     * @throws \app\common\exception\ServiceException
     */
    public function user_password()
    {
        if (Request::isAjax()) {
            $password = Request::param("password");
            return AdminService::userPassword($password) ? success_json_response("修改成功") : json_response("修改失败");
        } else {
            return $this->fetch();
        }
    }
}